package com.djl.seckilldemo.controller;

import com.djl.seckilldemo.pojo.User;
import com.djl.seckilldemo.service.IGoodsService;
import com.djl.seckilldemo.service.IUserService;
import com.djl.seckilldemo.vo.DetailVo;
import com.djl.seckilldemo.vo.GoodsVo;
import com.djl.seckilldemo.vo.RespBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @program: seckill-demo
 * @description: 商品controller
 * @author: Mr.dai
 * @create: 2021-10-15 14:58
 **/


//windows 优化前  1,835.311/sec
//linux上 优化前 184.612/sec
@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private RedisTemplate   redisTemplate;
    @Autowired
    //做thymeleaf手动渲染
    private ThymeleafViewResolver thymeleafViewResolver;
    /**
     * 跳转到商品列表页
     * @return
     */
    @RequestMapping(value = "/toList",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String toList( Model model, User user,HttpServletRequest request,HttpServletResponse response){
        //如果不为空则返回页面
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String html= (String) valueOperations.get("goodsList");
        if (!StringUtils.isEmpty(html)){
            return html;
        }
        model.addAttribute("user",user);
        model.addAttribute("goodsList",goodsService.findGoodsVo());
        //如果为空手动渲染并且存入redis
        WebContext webContext = new WebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap());
        html = thymeleafViewResolver.getTemplateEngine().process("goodsList",webContext);
        if (!StringUtils.isEmpty(html)){
            valueOperations.set("goodsList",html,60, TimeUnit.SECONDS);
        }

        return html;

        //return "goodsList";

    }


    /**
     * 跳转商品详情页面
     * @param goodsId
     * @return
     */
    @RequestMapping( "/toDetail/{goodsId}")
    @ResponseBody
    public RespBean toDetail(@PathVariable Long goodsId, Model model, User user){


        GoodsVo goodsVo = goodsService.findGoodsVoByGoodsId(goodsId);
        model.addAttribute("goods",goodsVo);
        Date startDate = goodsVo.getStartDate();
        Date endDate = goodsVo.getEndDate();
        Date nowDate = new Date();
        //秒杀状态
        int secKillStatus=0;
        //秒杀倒计时
        int remainSeconds=0;
        //秒杀还未开始
        if (nowDate.before(startDate)){
            remainSeconds=(int)((startDate.getTime()-nowDate.getTime())/1000);
        }
        //秒杀已经结束
        else if (nowDate.before(endDate)){
            secKillStatus=2;
            remainSeconds=-1;
        }else
        //
        {
            secKillStatus=1;
            remainSeconds=-1;
        }
        DetailVo detailVo = new DetailVo();
        detailVo.setUser(user);
        detailVo.setGoodsVo(goodsVo);
        detailVo.setSeckillStatus(secKillStatus);
        detailVo.setRemainSeconds(remainSeconds);
        return  RespBean.success(detailVo);




    }

    /**
     * 跳转商品详情页面
     * @param goodsId
     * @return
     */
    @RequestMapping(value = "/toDetail2/{goodsId}",produces = "text/html;chartset=utf-8")
    @ResponseBody
    public String toDetail2(@PathVariable Long goodsId,Model model,User user,HttpServletRequest request,HttpServletResponse response){
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String html=(String) valueOperations.get("goodsDetail:"+goodsId);
        if (!StringUtils.isEmpty(html)){
            return html;
        }

        model.addAttribute("user",user);
        GoodsVo goodsVo = goodsService.findGoodsVoByGoodsId(goodsId);
        model.addAttribute("goods",goodsVo);
        Date startDate = goodsVo.getStartDate();
        Date endDate = goodsVo.getEndDate();
        Date nowDate = new Date();
        //秒杀状态
        int secKillStatus=0;
        //秒杀倒计时
        int remainSeconds=0;
        //秒杀还未开始
        if (nowDate.before(startDate)){
            remainSeconds=(int)((startDate.getTime()-nowDate.getTime())/1000);
        }
        //秒杀已经结束
        else if (nowDate.before(endDate)){
            secKillStatus=2;
            remainSeconds=-1;
        }else
        //
        {
            secKillStatus=1;
            remainSeconds=-1;
        }
        model.addAttribute("seckillStatus",secKillStatus);
        model.addAttribute("remainSeconds",remainSeconds);
        WebContext webContext = new WebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap());
        html=thymeleafViewResolver.getTemplateEngine().process("goodsDetail",webContext);
        if (!StringUtils.isEmpty(html)){
            valueOperations.set("goodsDetail:"+goodsId,html,60, TimeUnit.SECONDS);
        }
        return html;
    }
}
